package fr.insalyon.sims.broadcast.tests;

import java.awt.Dimension;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;

import javax.swing.JFrame;

import fr.insalyon.sims.broadcast.network.DataErrorEvent;
import fr.insalyon.sims.broadcast.network.DataErrorListener;
import fr.insalyon.sims.broadcast.network.TCPStreamReceiver;
import fr.insalyon.sims.broadcast.service.BroadcastReceiverService;
import fr.insalyon.sims.broadcast.service.BroadcastTransmitterService;
import fr.insalyon.sims.broadcast.tools.DisplayPanel;
import fr.insalyon.sims.broadcast.tools.ImageBuilder;
import fr.insalyon.sims.streamManagerClient.Response;

public class FullTest {
	// ------------------------------------------------------- CONSTANTS -- //
	// ------------------------------------------------------ ATTRIBUTES -- //
	// ---------------------------------------------------- CONSTRUCTORS -- //
	// --------------------------------------------------------- METHODS -- //

	public static void main(String[] args) throws UnknownHostException {
		int srcPort = 22593;
		int destPort = 22594;
		String multicastIp = "224.0.0.1";
		int multicastPort = 22594;
		
		// Create image display stuff
		ImageBuilder builder = new ImageBuilder();
		DisplayPanel panel = new DisplayPanel(builder);
		panel.start();
		
		JFrame frame = new JFrame("Display");
		frame.add(panel);
		frame.setSize(new Dimension(600, 500));
		frame.setVisible(true);
		
		// Create TCP receiver stream
		TCPStreamReceiver receiver = new TCPStreamReceiver(destPort);
		receiver.addDataListener(builder);
		receiver.addDataErrorListener(new DataErrorListener() {
			public void streamError(DataErrorEvent event) {
				System.out.println("[receiver] an exception occured!");
				event.getException().printStackTrace();
			}
		});
		
		// Start the stream
		receiver.connect();
		receiver.start();
		System.out.println("[receiver] started");
		
		// Receiver service parameters
		HashMap<String, String> receiverParams = new HashMap<String, String>();
		receiverParams.put("destIp", "127.0.0.1");
		receiverParams.put("destPort", String.valueOf(destPort));
		receiverParams.put("multicastIp", multicastIp);
		receiverParams.put("multicastPort", String.valueOf(multicastPort));
		
		// Create and start the receiver service
		BroadcastReceiverService receiverService = new BroadcastReceiverService();
		Response receiverResponse = receiverService.doStart(receiverParams);
		System.out.println("[receiver-service] started (response: " + receiverResponse + ")");
		
		// Transmitter service parameters
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("multicastIp", multicastIp);
		params.put("multicastPort", String.valueOf(multicastPort));
		
		// Create and start the transmitter service
		BroadcastTransmitterService transmitterService =
			new BroadcastTransmitterService(srcPort,
					InetAddress.getByName(multicastIp), multicastPort);
		Response transmitterResponse = transmitterService.doStart(params);
		System.out.println("[transmitter-service] started (response: " + transmitterResponse + ")");
	}
	
	// ------------------------------------------------- PRIVATE METHODS -- //
	// --------------------------------------------------- INNER CLASSES -- //
}
